    /*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GUI;

import java.util.ArrayList;
import javax.swing.ComboBoxModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Asko
 */
public class ChannelsContainer implements ComboBoxModel<Object> {

    ArrayList<ControlChannel> channels = new ArrayList<>();
    ArrayList<ListDataListener> dataList = new ArrayList<>();
    ControlChannel  selectedControlCh = null ;

    public void add(ControlChannel ch) {
        channels.add(ch);
        changed();
    }

    public ControlChannel get(int chNum) {
        return channels.get(chNum);
    }

    public void remove(int chNum) {
        channels.remove(chNum);
        changed();
    }

    public void remove(ControlChannel obj) {
        channels.remove(obj);
        changed();
    }

    public void removeAll() {
        channels.clear();
        changed();
    }

    public int size() {
        return channels.size();
    }

    @Override
    public int getSize() {
        return size();
    }

    @Override
    public Object getElementAt(int index) {
        return get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {
        dataList.add(l);
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
        dataList.remove(l);
    }

    private void changed() {
        for (int i = 0; i < dataList.size(); i++) {
            dataList.get(i).contentsChanged(new ListDataEvent(this,
                    ListDataEvent.CONTENTS_CHANGED, 0, dataList.size()));
        }
    }

    @Override
    public void setSelectedItem(Object anItem) {
                 this.selectedControlCh = (ControlChannel)anItem ;
    }

    @Override
    public Object getSelectedItem() {
        return selectedControlCh ;
    }
}
